<?php

class Event_controller extends CI_Controller {
    
    function Event_controller() {
        parent::__construct();
        
        $this->load->model('Event_model');
    }
    
    public function index() {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');
        
        $this->load->model('Event_model');
        $this->load->model('Language_model');

        $data['route'] = 'events';
        $language_id = $this->session->userdata('language_id');
        if(empty($language_id)) $language_id = 1;
        $data['events'] = $this->Event_model->events_list($language_id);
        $data['languages'] = $this->Language_model->languages_list();
        
        $this->load->view('layout/admin', $data);
    }
    
    public function listEvents() {
        $this->load->view('event/index_view');      
    }
    
    public function get($event_id) {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $language_id = $this->session->userdata('language_id');
        if(empty($language_id)) $language_id = 1;

        $this->load->model('Multimedia_model');
        $multimedia = $this->Multimedia_model->multimedia_home_list();
        if(!empty($multimedia)) {
            $data['multimedia'] = $multimedia;
        }

        $this->load->model('Event_model');
        $data['event'] = $this->Event_model->event_get($event_id);

        $this->load->view('event/index_detail', $data);
    }
    
    public function delete($event_id) {
        $this->Event_model->event_delete($event_id);
        
        $this->index();
    }
    
    public function update($event_id) {
        $data = $this->input->post();
        
        $this->Event_model->event_update($event_id, $data['language_id'], $data['name'], $data['cover'], $data['content']);
        
        $this->index();
    }
    
    public function upload() {
        if($this->input->post('upload')) {
            $this->load->library('session');
            $data['lang'] = $this->session->userdata('lang');
            $data['member_id'] = $this->session->userdata('member_id');
            $data['firstname'] = $this->session->userdata('firstname');

            $data['route'] = 'add_event';
            $data['error'] = $this->upload->display_errors();
            
            $this->load->view('layout/admin', $data);
        } else {
            $this->do_upload();
        }
    }
    
    function do_upload() {
        $config['upload_path'] = UPLOADPATH.'\event';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = true;

        $this->load->library('upload', $config);
        
        if (!$this->upload->do_upload()) {
            $this->load->library('session');
            $data['lang'] = $this->session->userdata('lang');
            $data['member_id'] = $this->session->userdata('member_id');
            $data['firstname'] = $this->session->userdata('firstname');

            $data['route'] = 'add_event';
            $data['error'] = $this->upload->display_errors();
            
            $this->load->view('layout/admin', $data);
        } else {
            $data = array('upload_data' => $this->input->post());
            
            $this->set($this->input->post('language'), $this->input->post('name'), $this->upload->data(), $this->input->post('content'));

            $this->load->view('layout/upload_success', $data);
            $this->index();
        }
    }
    
    public function set($language_id, $name, $upload_data, $content) {
        $data = $this->input->post();
        
        $this->Event_model->event_set($language_id, $name, $upload_data['file_name'], $content);
    }
    
    public function subscribe() {
        $data = $this->input->post();
        
        $this->Event_model->event_subscribe($data['email']);
        
        redirect('','refresh');
    }
    
    public function unubscribe() {
        $data = $this->input->post();
        
        $this->Event_model->event_unsubscribe($data['email']);
        
        redirect('','refresh');
    }    
    
}

?>